package service.impl.v2;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.ejb.TransactionManagement;
import javax.transaction.Transaction;

import org.springframework.aop.framework.AopProxy;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.TransactionInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import dao.v2.ConfigVOMapper;
import dao.v2.FileVOMapper;
import model.FileVO;
import service.v2.MemberChildService;
import service.v2.MemberService;
import service.v2.QRCodeService;
import service.v2.WeChatService;
import util.PropertieUtil;
import util.WeixinUtil;



@Service
public class QRCodeServiceImpl implements QRCodeService {
	
	@Autowired
	private ConfigVOMapper configVOMapper;
	
	@Autowired
	private FileVOMapper fileVOMapper;
	
	@Autowired
	private MemberChildService memberchildservice;
	@Autowired
	private WeChatService wechatservice;
	
	public String getQRCodeUrl(String member_id){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		Date date=new Date();  
		String now=sdf.format(date); 
		String confValue = configVOMapper.loadConfig("ACCESS_TOKEN");
		JSONObject conf =JSON.parseObject(confValue); 
		String access_token = null;
		String QRCode_ticket = null;
		String fileName = null;
		
		access_token = wechatservice.getAccessToken();
	    QRCode_ticket =  WeixinUtil.getTempQRCode(access_token,member_id);
	    
	   try {
			fileName =  WeixinUtil.getQRCodeImg(QRCode_ticket);
			FileVO file  = new FileVO();
			file.setPath(fileName);
			file.setCat_id(PropertieUtil.loadPropertie("qrcode_cat"));
			file.setTemp_yn("Y");
			fileVOMapper.addFile(file);
			
			configVOMapper.updConfValueByKey("QRCODE_URL",fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileName;
	}

	public void testTransanction() {
		configVOMapper.TestTrasnaction();
		test();
		memberchildservice.test1();
	}
	
	public void test(){
		configVOMapper.TestTrasnaction();
		/*String a = "";
		Integer.parseInt(a);*/
	}
}
